package com.max.xihu.concurrent.day01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author: Max
 * @Date: 2020-07-05 02:29
 * @Description:
 */
public class DefaultThreadPool<Job extends Runnable> implements ThreadPool{

    private static final int MAX_WORKER_NUMBERS = 10;

    private static final int DEFAULT_WORKER_NUMBERS = 5;

    private static final int MIN_WORKER_NUMBERS = 1;

    private final LinkedList<Job> jobs = new LinkedList<>();

    private final List<Worker> workers = Collections.synchronizedList(new ArrayList<>());

    private int workerNum = DEFAULT_WORKER_NUMBERS;

    private AtomicLong threadNum = new AtomicLong();

    private DefaultThreadPool(){
        initWorker(DEFAULT_WORKER_NUMBERS);
    }

    public DefaultThreadPool(int num){
        workerNum = num > MAX_WORKER_NUMBERS
                ? MAX_WORKER_NUMBERS
                : num < MIN_WORKER_NUMBERS
                ? MIN_WORKER_NUMBERS
                : num;
        initWorker(num);
    }




    @Override
    public void execute(Runnable job) {

        if (job != null){
            synchronized (jobs){
//                jobs.addLast(job);
                jobs.notify();
            }
        }


    }

    @Override
    public void shutDown() {
        for (Worker worker : workers){
            worker.shutdown();
        }

    }

    @Override
    public void addWorkers(int num) {

        synchronized (jobs){
            if (num + this.workerNum > MAX_WORKER_NUMBERS){
                num = MAX_WORKER_NUMBERS - this.workerNum;
            }
            initWorker(num);
            this.workerNum += num;
        }
    }

    @Override
    public void removeWorker(int num) {

        synchronized (jobs){
            if ( num >= this.workerNum){
                throw new IllegalArgumentException("beyound workNumber");
            }
            int count = 0;
            while (count < num){
                Worker worker = workers.get(count);
                if (workers.remove(worker)){
                    worker.shutdown();
                    count++;
                }
            }
            this.workerNum -= count;
        }
    }

    @Override
    public int getJobSize() {
        return jobs.size();
    }



    private void initWorker(int num){
        for (int i = 0; i < num; i++){
            Worker worker = new Worker();
            workers.add(worker);
            Thread thread = new Thread(worker,"ThreadPool-Worker-"+threadNum.incrementAndGet());
            thread.start();
        }
    }

    class Worker implements Runnable{

        private volatile boolean running = true;

        @Override
        public void run() {

            while (running){

                Job job = null;

                synchronized (jobs){

                    while (jobs.isEmpty()){
                        try {
                            jobs.wait();
                        }catch (InterruptedException e){
                            Thread.currentThread().interrupt();;
                            return;
                        }
                    }

                    job = jobs.removeFirst();
                }

                if (job != null){
                    try {
                        job.run();
                    }catch (Exception ex){

                    }
                }
            }

        }

        public void shutdown(){
            running = false;
        }
    }
}
